package com.hunan.api.catv.service.biz.account;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hunan.api.catv.common.ConstantsMsg;
import com.hunan.api.catv.entity.ServiceResult;
import com.hunan.api.catv.entity.po.MemberWithdrawRecord;
import com.hunan.api.catv.entity.vo.MemberWithdrawRecordVo;
import com.hunan.api.catv.service.ResultService;
import com.hunan.api.catv.service.impl.account.AccountWithdrawService;
import com.hunan.api.catv.utils.EasyExportUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @Author: wangchao
 * @Date: 2020/10/27 9:15
 */
@Service
public class AccountWithdrawBiz extends ResultService {

    @Autowired
    AccountWithdrawService accountWithdrawService;


    /**
     * 获取用户提现列表
     */
    public PageInfo<MemberWithdrawRecordVo> getMemberWithdrawList(int pageNum, int pageSize, String nickName, Integer status, Integer deleted, String startCreateTime, String endCreateTime) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(getMemberWithdraws(nickName, status, deleted, startCreateTime, endCreateTime));
    }

    public ServiceResult saveMemberWithdrawInfo(MemberWithdrawRecord memberWithdrawRecord) {
        boolean insert = memberWithdrawRecord.setCreateTime(new Date()).setUpdateTime(new Date()).insert();
        if (!insert) {
            return buildFail(false, ConstantsMsg.admin_add_fail);
        }
        return buildOK(true, ConstantsMsg.admin_add_success);
    }

    private List<MemberWithdrawRecordVo> getMemberWithdraws(String nickName, Integer status, Integer deleted, String startCreateTime, String endCreateTime) {
        MemberWithdrawRecordVo memberWithdrawRecordVo = new MemberWithdrawRecordVo();
        memberWithdrawRecordVo.setNickName(nickName);
        memberWithdrawRecordVo.setStatus(status);
        memberWithdrawRecordVo.setDeleted(deleted);
        memberWithdrawRecordVo.setStartCreateTime(startCreateTime);
        memberWithdrawRecordVo.setEndCreateTime(endCreateTime);
        return accountWithdrawService.getMemberWithdrawList(memberWithdrawRecordVo);
    }

    public ServiceResult exportMemberWithdraw(String nickName, Integer status, Integer deleted, String startCreateTime, String endCreateTime, HttpServletResponse response) {

        EasyExportUtils.exportExcel(getMemberWithdraws(nickName, status, deleted, startCreateTime, endCreateTime), "", "sheet1", MemberWithdrawRecordVo.class, "用户提现成功记录.xlsx", response);
        return buildOK(true);
    }
}
